<template>
    <view>
		<view class="page-section">
			<search-bar :clearable="false" 
						:showCamera="false" 
						:value="searchText"
						@tap="goToPage('/pages/health/food/search/search?text='+searchText)">
			</search-bar>
		</view>
        <view class="dividingLine"></view>
        <view class="forFoodsTypes" style="flex-direction: column" >
            <view class="cardBackground" v-for="(item,index) in searchResult":key="item.pageId">
                <image class="foodsTypesImgs" :src="item.pic"></image>
                <text class="foodsTypesNames" >{{item.name}}</text>
                <text @tap="addFoodsCards(item.id)" class="addFoodsCard">添加打卡</text>
                <uni-icons  type="forward" class="addFoodsCardImg" size="32" @tap="addFoodsCards(item.id)"></uni-icons>
            </view>
        </view>
    </view>
</template>

<script>
import SearchBar from "@/components/SearchBar.vue"
export default {
	components:{SearchBar},
    data() {
        return {
            searchText: null,
			searchResult:[],
			page:0,
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		this.searchText=options.text
		var urlstr = '/foodInfo/get/name=' + this.searchText + '/page=' + this.page
		this.$https({
			url: this.$baseUrl + urlstr,
			success: (res) => {
				if (res.statusCode == 200) {
					for(let i=0;i<res.data['data'].length;i++){
						this.searchResult.push({
							pageId: i,
							'category': res.data['data'][i].category,
							'cellulose': res.data['data'][i].cellulose,
							'energy': res.data['data'][i].energy,
							'fat': res.data['data'][i].fat,
							'id': res.data['data'][i].id,
							'name': res.data['data'][i].name,
							'pic': res.data['data'][i].pic,
							'protein': res.data['data'][i].protein,
							'state': res.data['data'][i].state,
							'sugar': res.data['data'][i].sugar,
							'version': res.data['data'][i].version,
						});
					}
				} else {
					uni.showToast({
						title: res.data.errorCode,
						icon: 'error',
					})
				}
			},
		
			fail: (e) => {
				console.log(e)
				uni.showToast({
					title: '搜索失败',
					icon: 'error',
				})
			}
		});
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        /**
         * 微信搜索跳转的接口函数
         * */
        searchResults: function () {
            console.log(this.searchText);
            uni.navigateTo({
                url: '/pages/health/food/results/results'
            });
        },

        inputSearchText: function (e) {
            console.log(e.detail.value);
            this.setData({
                searchText: e.detail.value
            });
        },

        addFoodsCards: function (e) {
            uni.navigateTo({
                url: '/pages/health/food/details/details?id='+e
            });
        },
		
		/*
		 跳转到打卡界面
		*/
		goToPage(url){
			console.log(url)
			uni.navigateTo({
			    url: url
			});
		}
    }
};
</script>
<style>
@import './results.css';
</style>
